package edu.zju;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class ThreadManager {
	private Map<String, Thread> map = new HashMap<String, Thread>();

	private ThreadManager() {
	}

	private static ThreadManager mnger = null;

	public static ThreadManager newInstance() { // FIXME: not consider
												// concurrency
		if (mnger == null) {
			mnger = new ThreadManager();
		}
		return mnger;
	}

	public void putListener(String port, Thread t) {
		map.put(port, t);
	}

	public void killAllListener() {
		Collection<Thread> c = map.values();
		Iterator<Thread> it = c.iterator();
		for (; it.hasNext();) {
			Thread t = it.next();
			t.stop();
		}
	}

	public void KillListener(String usbPort) {
		Set<String> key = map.keySet();
		for (Iterator<String> it = key.iterator(); it.hasNext();) {
			String s = (String) it.next();
			if (s.equals(usbPort)){
				Thread t = map.get(s);
				t.stop();
			}
		}
	}

	public static void main(String args[]) {
	}
}
